package com.nmgdz.bigdata.controller;

import com.nmgdz.bigdata.entity.User;
import com.nmgdz.bigdata.service.RedisService;
import com.nmgdz.bigdata.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @auther: xhd
 * @Date: 2020/9/1/11:15
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @GetMapping("/queryById")
    public String queryById(int id) {
        //从redis获取key为Id User
        String userStr = redisService.get(id + "");
        //判断是否存在
        if (userStr != null) {
            //查询 设置到redis
            redisService.set(id + "", userService.queryById(id).toString());
        }
        return userStr;
    }


    /**
     * 增 改
     * @param user
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public User saveOrUpdateUser(User user) {
        return userService.save(user);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Boolean deleteUser(int id) {
        userService.deletUserById(id);
        return true;
    }

}
